﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Jbe.TestSuite.TestDevices.Interface;
using Jbe.Composite.Extensions;
using System.Windows.Input;
using Jbe.TestSuite.TestDevices.Manager.Properties;

namespace Jbe.TestSuite.TestDevices.Manager.Applications
{
    public class TestDevicesPresentationModel : PresentationModel<ITestDevicesView>, IHeaderInfoProvider
    {
        private readonly ITestDeviceManager testDeviceManager;
        private readonly DelegateCommand openDeviceCommand;
        private readonly DelegateCommand removeDeviceCommand;


        public TestDevicesPresentationModel(ITestDevicesView view, ITestDeviceManager testDeviceManager)
            : base(view)
        {
            this.testDeviceManager = testDeviceManager;
            this.openDeviceCommand = new DelegateCommand(OpenDevice, CanOpenDevice);
            this.removeDeviceCommand = new DelegateCommand(RemoveDevice, CanRemoveDevice);

            view.Model = this;
        }

        public string HeaderInfo
        {
            get { return Resources.TestDevices; }
        }

        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/DeviceManager.ico")); }
        }

        public ICommand OpenDeviceCommand { get { return openDeviceCommand; } }

        public ICommand RemoveDeviceCommand { get { return removeDeviceCommand; } }

        public IList<IControlView> ControlViews { get { return testDeviceManager.ControlViews; } }


        public void ControlViewsSelectionChanged()
        {
            openDeviceCommand.RaiseCanExecuteChanged();
            removeDeviceCommand.RaiseCanExecuteChanged();
        }

        private bool CanOpenDevice()
        {
            return View.SelectedControlViews.Any();
        }

        private void OpenDevice()
        {
            testDeviceManager.OpenDevice(View.SelectedControlViews);
        }

        private bool CanRemoveDevice()
        {
            return View.SelectedControlViews.Any();
        }

        private void RemoveDevice()
        {
            testDeviceManager.RemoveDevice(View.SelectedControlViews);
        }
    }
}
